package com.itljm.cxrMarket.controller;

import com.itljm.cxrMarket.common.Result;
import com.itljm.cxrMarket.entity.dto.AddToCartRequestDTO;
import com.itljm.cxrMarket.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ljm
 * @date 2024/12/27 9:24
 */
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    // 添加商品到购物车
    @RequestMapping("/add")
    public Result add(@RequestBody AddToCartRequestDTO addToCartRequestDTO) {
        boolean b = shoppingCartService.addProductToCart(addToCartRequestDTO.getUserId(), addToCartRequestDTO.getProductId());
        return b ? Result.ok("添加成功") : Result.fail("库存不足");
    }
    // 增加购物车商品数量
    @RequestMapping("/increase")
    public Result increase(@RequestBody AddToCartRequestDTO entity) {
        boolean b = shoppingCartService.increaseProductQuantity(entity.getUserId(), entity.getProductId(),entity.getQuantity());
        return b ? Result.ok("增加成功") : Result.fail("增加失败");
    }
    // 减少购物车商品数量
    @RequestMapping("/decrease")
    public Result decrease(@RequestBody AddToCartRequestDTO entity) {
        boolean b = shoppingCartService.decreaseProductQuantity(entity.getUserId(), entity.getProductId());
        return b ? Result.ok("减少成功") : Result.fail("减少失败");
    }
    // 删除购物车商品
    @RequestMapping("/delete")
    public Result delete(@RequestBody AddToCartRequestDTO entity) {
        boolean b = shoppingCartService.removeProductFromCart(entity.getUserId(), entity.getProductId());
        return b ? Result.ok("删除成功") : Result.fail("删除失败");
    }
    // 获取个人购物车
    @RequestMapping("/getMyShoppingCart")
    public Result getMyShoppingCart(Long userId) {
        return Result.ok("获取成功", shoppingCartService.getShoppingCartItems(userId));
    }
    // 同步更新购物车商品选中状态
    @RequestMapping("/updateCheckedStatus")
    public Result updateSelectStatus(@RequestBody AddToCartRequestDTO entity) {
        boolean b = shoppingCartService.updateCheckedStatus(entity.getUserId(), entity.getProductId(), entity.getChecked());
        return b ? Result.ok() : Result.fail();
    }
}
